package role

import (
	"fmt"
	"sanguo/node/common/actionEvent"
	"sanguo/node/common/attr"
	"strconv"
	"strings"
	"time"
)

//经验
func GiveExp(arg ...interface{}) {
	role_ := arg[0].(*Role)
	param := arg[1].([]map[string]string)
	if len(param) > 0 {
		count, err := strconv.Atoi(param[0]["1"])
		if nil != err {
			//log
			return
		}
		if count > 0 {
			role_.AddExp(int64(count))
		}
	}
}

//星币
func GiveGold(arg ...interface{}) {
	role_ := arg[0].(*Role)
	param := arg[1].([]map[string]string)
	if len(param) > 0 {
		count, err := strconv.Atoi(param[0]["1"])
		if nil != err {
			//log
			return
		}
		if count > 0 {
			role_.AddGold(int64(count))
		}
	}
}

//体力
func GiveActionPoint(arg ...interface{}) {
	role_ := arg[0].(*Role)
	param := arg[1].([]map[string]string)
	if len(param) > 0 {
		count, err := strconv.Atoi(param[0]["1"])
		if nil != err {
			//log
			return
		}
		if count > 0 {
			role_.AddActionPoint(int64(count))
		}
	}
}

func GiveDiamond(arg ...interface{}) {
	role_ := arg[0].(*Role)
	param := arg[1].([]map[string]string)
	if len(param) > 0 {

		count, err := strconv.Atoi(param[0]["1"])
		if nil != err {
			//log
			return
		}
		if count > 0 {
			role_.AddDiamond(int64(count))
		}
	}
}

//道具
func GiveItem(arg ...interface{}) {
	role_ := arg[0].(*Role)
	items := arg[1].([]map[string]string)
	var err error
	var id, count int
	for _, v := range items {
		id, err = strconv.Atoi(v["1"])
		if nil != err {
			//log
			return
		}
		count, err = strconv.Atoi(v["2"])

		if nil != err {
			//log
			return
		}

		if count > 0 {
			items := make(map[int32]int32)
			items[int32(id)] = int32(count)
			role_.AddItem(items)
		}
	}
}

//武器
func GiveWeapon(arg ...interface{}) {
	role_ := arg[0].(*Role)
	Weapons := arg[1].([]map[string]string)
	var err error
	var id, count int
	for _, v := range Weapons {
		id, err = strconv.Atoi(v["1"])
		if nil != err {
			//log
			return
		}
		count, err = strconv.Atoi(v["2"])

		if nil != err {
			//log
			return
		}

		if count > 0 {
			weapons := make(map[int32]int32)
			weapons[int32(id)] = int32(count)
			role_.AddWeapon(weapons)
		}
	}
}

func GiveWeaponShard(arg ...interface{}) {
	role_ := arg[0].(*Role)
	WeaponShards := arg[1].([]map[string]string)
	var err error
	var id, count int
	for _, v := range WeaponShards {
		id, err = strconv.Atoi(v["1"])
		if nil != err {
			//log
			return
		}
		count, err = strconv.Atoi(v["2"])

		if nil != err {
			//log
			return
		}
		if count > 0 {
			weaponShards := make(map[int32]int32)
			weaponShards[int32(id)] = int32(count)
			role_.AddWeaponShard(weaponShards)
		}
	}
}

//武魂
func GiveSoul(arg ...interface{}) {
	role_ := arg[0].(*Role)
	Souls := arg[1].([]map[string]string)
	var err error
	var id, count int
	for _, v := range Souls {
		id, err = strconv.Atoi(v["1"])
		if nil != err {
			//log
			return
		}
		count, err = strconv.Atoi(v["2"])

		if nil != err {
			//log
			return
		}
		if count > 0 {
			souls := make(map[int32]int32)
			souls[int32(id)] = int32(count)
			role_.AddSoul(souls)
		}
	}
}

//解锁boss副本
func UnLockBossZone(arg ...interface{}) {
	role_ := arg[0].(*Role)
	param := arg[1].([]map[string]string)
	var err error
	var bossZoneID int
	for _, v := range param {
		bossZoneID, err = strconv.Atoi(v["1"])
		if nil != err {
			//log
			return
		}

		role_.GetRoleBossZone().AddBossZone(int32(bossZoneID))
		role_.OnBossZoneDirty()
	}
}

func LevelLimit(arg ...interface{}) bool {
	role_ := arg[0].(*Role)
	param := arg[1].([]map[string]string)
	if len(param) > 0 {
		level, err := strconv.Atoi(param[0]["1"])
		if nil != err {
			return false
		}

		currentLevel, _ := role_.GetRoleAttr().GetAttr(attr.Level)
		if currentLevel < int64(level) {
			return false
		}
	}
	return true
}

func ItemLimit(arg ...interface{}) bool {
	role_ := arg[0].(*Role)
	items := arg[1].([]map[string]string)
	var err error
	var id, count int
	for _, v := range items {
		id, err = strconv.Atoi(v["1"])
		if nil != err {
			//log
			return false
		}
		count, err = strconv.Atoi(v["2"])

		if nil != err {
			//log
			return false
		}

		item := role_.GetRoleItem().GetItem(int32(id))
		if item == nil {
			return false
		}
		if item.ItemCount < int32(count) {
			return false
		}
	}
	return true
}

func TimeLimit(arg ...interface{}) bool {
	param := arg[1].([]map[string]string)
	WeekDay := map[string]int{
		"Monday":    1,
		"Tuesday":   2,
		"Wednesday": 3,
		"Thursday":  4,
		"Friday":    5,
		"Saturday":  6,
		"Sunday":    7,
	}

	startStr := strings.Split(param[0]["1"], "@")
	endStr := strings.Split(param[0]["2"], "@")

	startWeekDay, _ := strconv.Atoi(startStr[0])
	endWeekDay, _ := strconv.Atoi(endStr[0])

	startTime, _ := time.ParseInLocation("2006-01-02 15:04:05", fmt.Sprintf("%s %s", time.Now().Format("2006-01-02"), startStr[1]), time.Local)
	endTime, _ := time.ParseInLocation("2006-01-02 15:04:05", fmt.Sprintf("%s %s", time.Now().Format("2006-01-02"), endStr[1]), time.Local)

	weekDay := time.Now().Weekday().String()
	now := time.Now()
	if startWeekDay <= WeekDay[weekDay] && WeekDay[weekDay] <= endWeekDay {
		if now.After(startTime) && now.Before(endTime) {
			return true
		}
	}

	return false
}

func init() {
	actionEvent.Register("GiveExp", GiveExp)
	actionEvent.Register("GiveGold", GiveGold)
	actionEvent.Register("GiveActionPoint", GiveActionPoint)
	actionEvent.Register("GiveDiamond", GiveDiamond)
	actionEvent.Register("GiveItem", GiveItem)
	actionEvent.Register("GiveWeapon", GiveWeapon)
	actionEvent.Register("GiveWeaponShard", GiveWeaponShard)
	actionEvent.Register("GiveSoul", GiveSoul)
	actionEvent.Register("UnLockBossZone", UnLockBossZone)
	actionEvent.LimitRegister("LevelLimit", LevelLimit)
	actionEvent.LimitRegister("TimeLimit", TimeLimit)
	actionEvent.LimitRegister("ItemLimit", ItemLimit)

}
